package com.zz.apk.service

import android.app.IntentService
import android.content.Intent
import android.text.TextUtils
import com.zz.apk.utils.DownloadUtil
import com.zz.apk.utils.FilesUtil

class DownloadService : IntentService(TAG) {
    companion object {
        const val TAG = "DownloadService"
        const val TAG_APK = "DownloadServiceApk"
        const val TAG_FILE = "DownloadServiceFLIE"
        const val DOWNLOAD_SERVICE_ACTION = "com.biz.download.message"
        const val DOWNLOAD_STATUS = "download_status"
        const val DOWNLOAD_URL = "download_url"
        const val DOWNLOAD_TAG = "download_tag"
        const val DOWNLOAD_CACHE_DIR = "download_cache_dir"
        const val DOWNLOAD_FILE_TYPE = "download_file_type"
        const val DOWNLOAD_PROGRESS = "download_progress"
        const val DOWNLOAD_TOTAL_SIZE = "download_total_size"
        const val DOWNLOAD_DOWNLOAD_SIZE = "download_download_size"
        const val DOWNLOAD_PATH = "download_path"
        const val RETRY_COUNT = 3
        const val STATUS_SUCCESS = 1
        const val STATUS_FAILED = 2
        const val STATUS_PREPARE = 3
        const val STATUS_PROGRESS = 4
        const val STATUS_EXISTS = 5
    }
    private var retryCount = 0
    override fun onHandleIntent(intent: Intent?) {
        val url = intent!!.getStringExtra(DOWNLOAD_URL)
        val tag = intent.getStringExtra(DOWNLOAD_TAG)
        var dir = intent.getStringExtra(DOWNLOAD_CACHE_DIR)
        val fileType = intent.getStringExtra(DOWNLOAD_FILE_TYPE)
        if (TextUtils.isEmpty(dir)) {
            dir = FilesUtil.FILE_SAVE_DOWNLOAD_DIR
        }
        if (!TextUtils.isEmpty(url)) {
            retryCount = 0
            download(url?:"", tag?:"", dir, fileType?:"")
        }
    }

    private fun download(url: String, tag: String, dir: String?, fileType: String) { //        每次都重新下载
        //String path = FilesUtil.getCacheFilePath(dir, url) + FilesUtil.getUrlEnd(url, fileType);
        //File fileNew = new File(path);
        //if (fileNew.exists() && fileNew.length() > 0) {
        //    sendMessage(STATUS_EXISTS, url, tag, path);
        //    return;
        //}
        val downloadUtil = DownloadUtil()
        downloadUtil.download(dir, url, fileType, object : DownloadUtil.OnDownloadListener {
            override fun onDownloadPrepare() {
                sendMessage(
                    STATUS_PREPARE, url, tag, null
                )
            }

            override fun onDownloadSuccess(path: String) {
                sendMessage(
                    STATUS_SUCCESS, url, tag, path
                )
            }

            override fun onDownloading(progress: Int, downLoadSize: Long, totalSize: Long) {
                val intent = Intent()
                intent.action = DOWNLOAD_SERVICE_ACTION
                intent.putExtra(
                    DOWNLOAD_STATUS, STATUS_PROGRESS
                )
                intent.putExtra(
                    DOWNLOAD_URL, url
                )
                intent.putExtra(
                    DOWNLOAD_TAG, tag
                )
                intent.putExtra(
                    DOWNLOAD_PROGRESS, progress
                )
                intent.putExtra(
                    DOWNLOAD_TOTAL_SIZE, totalSize
                )
                intent.putExtra(
                    DOWNLOAD_DOWNLOAD_SIZE, downLoadSize
                )
                sendBroadcast(intent)
            }

            override fun onDownloadFailed(e: Exception) { //下载失败，通过广播通知用户
                sendMessage(
                    STATUS_FAILED, url, tag, null
                )
            }
        })
    }

    private fun sendMessage(status: Int, url: String, tag: String, path: String?) {
        val intent = Intent()
        intent.action = DOWNLOAD_SERVICE_ACTION
        intent.putExtra(
            DOWNLOAD_STATUS, status
        )
        intent.putExtra(
            DOWNLOAD_URL, url
        )
        intent.putExtra(
            DOWNLOAD_TAG, tag
        )
        if (!TextUtils.isEmpty(path)) intent.putExtra(
            DOWNLOAD_PATH, path
        )
        sendBroadcast(intent)
    }


}